package edu.upf.opencities.web.dao.impl;

import org.apache.log4j.Logger;

import edu.upf.opencities.datamodel.*;
import edu.upf.opencities.web.connector.impl.ConnectorBooleanImpl;
import edu.upf.opencities.web.connector.impl.ConnectorImpl;
import edu.upf.opencities.web.connector.impl.ConnectorURLImpl;
import edu.upf.opencities.web.dao.ServiceDAO;
import edu.upf.opencities.web.dao.UserDAO;
import edu.upf.opencities.web.utils.Utils;

public class DAOImpl implements ServiceDAO, UserDAO {


	private static DAOImpl dao = null;
	public static Logger logger = Logger.getLogger(DAOImpl.class);
	public Utils utils = new Utils();
	public static ConnectorImpl connector = ConnectorImpl.getInstace();
	public static ConnectorURLImpl connectorURL = ConnectorURLImpl.getInstace();
	public static ConnectorBooleanImpl connectorBoolean = ConnectorBooleanImpl.getInstace();
	private String OSN_SERVER = "http://193.145.50.132";
	
	//Mock objects
	User user = new User();
	boolean first =true;
	
	//Singleton
	public static synchronized DAOImpl getInstace() {
		if (null == dao) {
			dao = new DAOImpl();
		}
		return dao;
	}
	
	@Override
	public Service registerService(Service service, User user) {
		connector.send(OSN_SERVER+"/OSN/rest/service/registerSecureService/", service, "service", user, "post");
		//connectorURL.executeURL(OSN_SERVER+"/OSN/rest/service/registerServiceURL/vic.valls@gmail.com/gato/TempService/s3cr3t/UPF/TemperatureService/10min/Environmental/2.5E/42.2N/cold,hot");
		return service;
	}
	
	public boolean updateService(Service service, User user){
			
		return connector.send(OSN_SERVER+"/OSN/rest/service/updateSecureService", service, "service", user, "post");
		
	}

	@Override
	public boolean deleteService(Service service, User user) {
		return connectorBoolean.executeBoolean(OSN_SERVER+"/OSN/rest/service/deleteService/"+utils.stringToUTF(user.getEmail())
				+"/"+utils.stringToUTF(user.getPassword())+"/"+service.getId(),"GET");	
	}

	@Override
	public Service getOneService(String idService) {
		return (Service) connectorURL.executeURL(OSN_SERVER+"/OSN/rest/service/getService/"+idService);
	}

	@Override
	public Services getServicesByProperty(String property, String content) {
		
		Services services = new Services();
		
		if(property.equals("mail")){
		    services = (Services) connectorURL.executeURL(OSN_SERVER+"/OSN/rest/service/getServicesByEmail/"+utils.stringToUTF(content));
		}
		else if(property.equals("group")){
			services = (Services) connectorURL.executeURL(OSN_SERVER+"/OSN/rest/service/getServicesByGroup/"+utils.stringToUTF(content));
		}
		else if(property.equals("tag")){
			services = (Services) connectorURL.executeURL(OSN_SERVER+"/OSN/rest/service/getServicesByTag/"+utils.stringToUTF(content));
		}
		return services;
	}

	@Override
	public String addEntry(Service service, Entry entry) {
		
		return "success";
	}

	@Override
	public Service getLastEntry(String idService) {
		return null;
	}

	@Override
	public void addTwitter(Service service, User user) {
	
	}

	@Override
	public Services getEntriesFromService(String idService, int numEntries) {
		return null;
	}

	//USER
	@Override
	public boolean registerUser(User user) {
		return connector.send(OSN_SERVER+"/OSN/rest/user/registerUser/", user, "user", user, "post");
	}

	@Override
	public boolean userUpdate(User user) {
		return connector.send(OSN_SERVER+"/OSN/rest/user/updateUser/", user, "user", user, "post");
	}

	@Override
	public User getUser(User user) {
		Object object = connectorURL.executeURL(OSN_SERVER+"/OSN/rest/user/getUser/"+utils.stringToUTF(user.getEmail()));
		return (User)object;
	}

	@Override
	public void deleteUser(User user) {
		String uri= OSN_SERVER+"/OSN/rest/user/deleteUser/"+utils.stringToUTF(user.getEmail())+"/"+utils.stringToUTF(user.getPassword());
		connectorURL.executeURL(uri);
	}

	@Override
	public boolean loginUser(User user) {
		String uri = OSN_SERVER+"/OSN/rest/user/loginUser/"
	+utils.stringToUTF(user.getEmail())
			+"/"+utils.stringToUTF(user.getPassword());
		
		return connectorBoolean.executeBoolean(uri,"GET");
	
	
	}
	

}
